(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.custAcctController', custAcctController);
	custAcctController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 
	                               'authService', 'ajaxServiceFactory', '$filter'];
	function custAcctController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, 
			authService, ajaxServiceFactory, $filter) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("custAcctLookup");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getCustAcctType : {
				url : 'do/cda/dataList/custAcctType',
				method : 'get'
			}
		});
		
		function getCustAcctTypeOptions() {
			var defer = $q.defer();
			ajaxService.getCustAcctType().then(function(response) {
				response.hasEmptyOption = true;
				defer.resolve(response);
			});
			return defer.promise;
		}
		
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			getCustAcctTypeOptions().then(function(response) {
				ctrl.custAcctTypeOptions = response;
			});
			
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			
			if (ctrl.dialogsModule.params.custCodeExactFlag) {
				ctrl.searchCriteria.custCodeExactFlag = ctrl.dialogsModule.params.custCodeExactFlag;// get params from parent page
			} else {
				ctrl.searchCriteria.custCodeExactFlag = '';
			}
			if (ctrl.dialogsModule.params.custCode) {
				ctrl.searchCriteria.custCode = $filter('encode')(ctrl.dialogsModule.params.custCode);
//				ctrl.searchCriteria.custCode = ctrl.dialogsModule.params.custCode;// get params from parent page
			} else {
				ctrl.searchCriteria.custCode = '';
			}
			if (ctrl.dialogsModule.params.custAcctType) {
				var tmp = ctrl.dialogsModule.params.custAcctType;
				if (tmp.indexOf(":") >= 0) {
					ctrl.searchCriteria.custAcctType = tmp.split(":")[1];
				} else {
					ctrl.searchCriteria.custAcctType = ctrl.dialogsModule.params.custAcctType;
				}
			} else {
				ctrl.searchCriteria.custAcctType = '';
			}
			if (ctrl.dialogsModule.params.ccy) {
				ctrl.searchCriteria.ccy = ctrl.dialogsModule.params.ccy;
			} else {
				ctrl.searchCriteria.ccy = '';
			}
			if (ctrl.dialogsModule.params.payCodeCat) {
				ctrl.searchCriteria.paycodeCat = ctrl.dialogsModule.params.payCodeCat;
			} else {
				ctrl.searchCriteria.paycodeCat = '';
			}
			if (ctrl.dialogsModule.params.crDr) {
				ctrl.searchCriteria.crAndDrflag = ctrl.dialogsModule.params.crDr;
			} else {
				ctrl.searchCriteria.crAndDrflag = '';
			}
			
			/*custAcctService.getCustAcctTypeOptions().then(function(response) {
				ctrl.custAcctTypeOptions = response;
			});*/
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);
